#include "FoGGEDEF.h"
#include "Name.h"

namespace _FOGGE_
{
	_UTIL_::Name Name_None;

	namespace _UTIL_
	{

		HashMap<foString,foUInt>* Name::s_ptrmapString=NULL;
		
		
		Name::Name(void)
		{
			m_ptrString=NULL;
			m_uiUniqID=0;
			
		}

		void Name::Finalize()
		{
			if(s_ptrmapString)
				foDELETE(s_ptrmapString);
		}
		Name::Name(LPCTSTR name)
		{
			
			foString strName=name;
			assert(name&&"Name::Name(LPCTSTR name)__ name is NULL");
			SetName(strName);
			
		}
		Name::Name(const foString& name)
		{
			SetName(name);
		}
		Name::Name(const Name& name)
		{
			m_ptrString = name.m_ptrString;
			m_uiUniqID = name.m_uiUniqID;
		}
		Name::~Name(void)
		{
		}

		const TCHAR * Name::c_str () const 
		{ 
			if(m_ptrString)
				return m_ptrString->c_str();
			else
				return __T("None");
		}

		const char * Name::c_strAnsi() 
		{ 
		#ifdef _UNICODE
			if(m_ptrString)
			{
				std::string ansi;
				_FO_UTIL_HELPER::UnicodeToAnsi(m_ptrString,ansi);
				return ansi->c_str();
			}
			else
				return "None";
		#else
			return c_str();
		#endif
		}

		void Name::CheckName(foString& name,HashMap<foString,foUInt>::iterator& iterResult)
		{
			iterResult=s_ptrmapString->find(name);
		}

		
		void Name::SetName(const foString& name)
		{
			HashMap<foString,foUInt>::iterator iterResult;

			if(s_ptrmapString==NULL)
				s_ptrmapString=foNEW(HashMap<foString,foUInt>);

			if(name.size()>0)
			{

				iterResult=s_ptrmapString->find(name);

				if(iterResult==s_ptrmapString->end())
				{//insert new name
					iterResult=s_ptrmapString->insert(HashMap<foString,foUInt>::value_type(name,s_ptrmapString->size()+1)).first;
				}

				m_ptrString=&iterResult->first;
				m_uiUniqID=iterResult->second;
				
			}
			else
			{//set to None
				m_ptrString=NULL;
				m_uiUniqID=0;
			}
		}

	}
}